// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <melon/init/init.h>
#include <gtest/gtest.h>

#include <pollux/connectors/hive/storage_adapters/s3fs/RegisterS3FileSystem.h>
#include <pollux/connectors/hive/storage_adapters/s3fs/tests/S3Test.h>
#include <pollux/connectors/hive/storage_adapters/test_common/InsertTest.h>
#include <pollux/dwio/parquet/register_parquet_reader.h>
#include <pollux/dwio/parquet/register_parquet_writer.h>

namespace kumo::pollux::filesystems {
namespace {

class S3InsertTest : public S3Test, public test::InsertTest {
 protected:
  static void SetUpTestCase() {
    memory::MemoryManager::testingSetInstance({});
  }

  void SetUp() override {
    S3Test::SetUp();
    filesystems::registerS3FileSystem();
    connector::registerConnectorFactory(
        std::make_shared<connector::hive::HiveConnectorFactory>());
    auto hiveConnector =
        connector::getConnectorFactory(
            connector::hive::HiveConnectorFactory::kHiveConnectorName)
            ->newConnector(
                ::exec::test::kHiveConnectorId,
                minioServer_->hiveConfig(),
                ioExecutor_.get());
    connector::registerConnector(hiveConnector);
    parquet::registerParquetReaderFactory();
    parquet::registerParquetWriterFactory();
  }

  void TearDown() override {
    parquet::unregisterParquetReaderFactory();
    parquet::unregisterParquetWriterFactory();
    connector::unregisterConnectorFactory(
        connector::hive::HiveConnectorFactory::kHiveConnectorName);
    connector::unregisterConnector(::exec::test::kHiveConnectorId);
    S3Test::TearDown();
    filesystems::finalizeS3FileSystem();
  }
};
} // namespace

TEST_F(S3InsertTest, s3InsertTest) {
  const int64_t kExpectedRows = 1'000;
  const std::string_view kOutputDirectory{"s3://writedata/"};
  minioServer_->addBucket("writedata");

  runInsertTest(kOutputDirectory, kExpectedRows, pool());
}
} // namespace kumo::pollux::filesystems

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  melon::Init init{&argc, &argv, false};
  return RUN_ALL_TESTS();
}
